package com.docios.cookbook2.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.docios.cookbook2.R;
import com.docios.cookbook2.entity.Category;
import com.docios.cookbook2.ui.otherPager.CommonChildActivity;
import com.docios.cookbook2.ui.otherPager.SearchFragment;

import java.util.List;

public class ChildCategoryAdapter extends RecyclerView.Adapter<ChildCategoryAdapter.ViewHolder> {
    List<Category.CList> cLists;
    Context childContext;

    public ChildCategoryAdapter(List<Category.CList> cLists,Context childContext){
        this.cLists=cLists;
        this.childContext=childContext;
    }

    @NonNull
    @Override
    public ChildCategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(childContext).inflate(R.layout.item_category_child, parent, false);
        return new ViewHolder(view);
    }

    /*
    * MAIN 上面都是OK的
    * */
    @Override
    public void onBindViewHolder(@NonNull ChildCategoryAdapter.ViewHolder holder, int position) {
        holder.name.setText(cLists.get(position).getName());
        //点击分类id查询方式跳转
        holder.name.setOnClickListener(v -> {
            Intent intent=new Intent(childContext, CommonChildActivity.class);
            intent.putExtra("flag","classid");
            intent.putExtra("classid",String.valueOf(cLists.get(position).getClassid()));
            childContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return cLists.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            name=itemView.findViewById(R.id.tv_cat_child);
        }
    }
}
